package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.entity.player.UserVehicleBone;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VehicleBoneConfigMgr extends TempMgr {
    /**
     * 魂骨回收奖励 key:星级
     */
    private static Map<Integer, Property> boneRecycleRewardMap = new HashMap<>();
    /**
     * 炼魂等级对应孔位
     */
    private static List<List<Integer>> levelPosList = new ArrayList<>();
    /**
     * 填充的槽位对应的基础资质
     */
    private static List<List<Integer>> holeQualityList = new ArrayList<>();
    /**
     * 填充总星数对应的资质加成
     */
    private static List<List<Integer>> starAmountAdditionList = new ArrayList<>();

    @Override
    public boolean init() throws Exception {
        addManagerReloadListener();
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        initConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public void addManagerReloadListener() {
        ConfigMgr.addReloadListener(() -> {
            initConfig();
        });
    }

    public static void initConfig() {
        Map<Integer, Property> tempBoneRecycleRewardMap = new HashMap<>();
        for (String s : GameConfig.SOUL_BONE_RECLAIM_PARAM.split("\\|")) {
            String[] split = s.split(",");
            int star = Integer.parseInt(split[0]);
            Property reward = PropertyHelper.parseStringToProperty(split[1]);
            tempBoneRecycleRewardMap.put(star, reward);
        }

        List<List<Integer>> tempLevelPosList = new ArrayList<>();
        for (String s : GameConfig.SOUL_BONE_UNLOCK_SLOT_PARAM.split("\\|")) {
            List<Integer> list = StringUtils.stringToIntegerList(s, ",");
            tempLevelPosList.add(list);
        }

        List<List<Integer>> tempHoleQualityList = new ArrayList<>();
        for (String s : GameConfig.SOUL_BONE_SUIT_SLOT_PARAM.split("\\|")) {
            List<Integer> list = StringUtils.stringToIntegerList(s, ",");
            tempHoleQualityList.add(list);
        }

        List<List<Integer>> tempStarAmountAdditionList = new ArrayList<>();
        for (String s : GameConfig.SOUL_BONE_SUIT_LEVEL_PARAM.split("\\|")) {
            List<Integer> list = StringUtils.stringToIntegerList(s, ",");
            tempStarAmountAdditionList.add(list);
        }

        boneRecycleRewardMap = tempBoneRecycleRewardMap;
        levelPosList = tempLevelPosList;
        holeQualityList = tempHoleQualityList;
        starAmountAdditionList = tempStarAmountAdditionList;
    }

    public static boolean isHoleUnlock(int boneLv, int pos) {
        if (pos < 1) {
            return false;
        }

        for (List<Integer> list : levelPosList) {
            int lvNeed = list.get(0);
            int unlockPos = list.get(1);

            if (boneLv >= lvNeed && pos <= unlockPos) {
                return true;
            }
        }

        return false;
    }

    public static int getVehicleBoneLvUnlockHoleNum(int boneLv) {
        int result = 0;
        for (List<Integer> list : levelPosList) {
            int lvNeed = list.get(0);
            int unlockPos = list.get(1);

            if (boneLv >= lvNeed) {
                result = unlockPos;
            } else {
                break;
            }
        }
        return result;
    }

    /**
     * 计算解锁的孔数量
     * @param boneHole 孔位解锁数据
     * @return
     */
    public static int calcVehicleBoneUnlockHoleNum(int boneHole) {
        int result = 0;
        //按二进制判断boneHold有几个1
        for (int i = 0; i < 32; i++) {
            if ((boneHole & (1 << i)) != 0) {
                result++;
            }
        }

        return result;
    }

    public static Property getBoneRecycleReward(int star) {
        return new Property(boneRecycleRewardMap.getOrDefault(star, new Property()));
    }

    public static Integer getBoneInitSkillId(GoodsInfo goodsInfo) {
        //710002#703001|703002
        String[] skillIdArr = goodsInfo.getExtendParam().split("#");
        //初始技能
        return Integer.parseInt(skillIdArr[0]);
    }

    public static int getHoleFullBaseQualification(int holeAmount) {
        int result = 0;
        for (List<Integer> list : holeQualityList) {
            int holeAmountNeed = list.get(0);
            int baseQuality = list.get(1);

            if (holeAmount >= holeAmountNeed) {
                result = baseQuality;
            } else {
                break;
            }
        }

        return result;
    }

    /**
     * 填满获得的星级加成
     * @param starAmount 累计共鸣点
     * @return
     */
    public static int calcBoneStarAmountAddition(int starAmount) {
        int addValue = 0;

        for (List<Integer> list : starAmountAdditionList) {
            int starAmountNeed = list.get(0);
            int addition = list.get(1);

            if (starAmount >= starAmountNeed) {
                addValue = addition;
            } else {
                break;
            }
        }
        return addValue;
    }

    /**
     * 计算共鸣加成
     * @param boneLv 炼魂等级
     * @param equipBoneList 装备的列表
     * @param boneHole 已解锁的孔
     * @return
     */
    public static int calcHoleFullAddQualification(int boneLv, List<UserVehicleBone> equipBoneList, int boneHole) {
        if (equipBoneList == null || equipBoneList.isEmpty()) {
            return 0;
        }
        int unlockHoleNum = VehicleBoneConfigMgr.calcVehicleBoneUnlockHoleNum(boneHole);
        if (equipBoneList.size() != unlockHoleNum) {//填满才有
            return 0;
        }
        int starAmount = 0;
        for (UserVehicleBone vehicleBone : equipBoneList) {
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(vehicleBone.getConfigId());
            if (goodsInfo != null) {
                starAmount += VehicleBoneConfigMgr.getBoneSuitStar(goodsInfo);
            }
        }

        return VehicleBoneConfigMgr.calcHoleFullAddQualification(boneLv, equipBoneList.size(), starAmount, boneHole);
    }

    public static int calcHoleFullAddQualification(int boneLv, int holeNum, int starAmount, int boneHole) {
        int unlockHoleNum = VehicleBoneConfigMgr.calcVehicleBoneUnlockHoleNum(boneHole);
        if (holeNum != unlockHoleNum) {//填满才有
            return 0;
        }
        int holeFullBaseQuality = getHoleFullBaseQualification(holeNum);
        int multiValue = VehicleBoneConfigMgr.calcBoneStarAmountAddition(starAmount);
        int holdFullAdd = BigDecimal.valueOf(holeFullBaseQuality).multiply(BigDecimal.valueOf(multiValue).divide(BigDecimal.valueOf(1000))).setScale(0, BigDecimal.ROUND_DOWN).intValue();
        return holdFullAdd;
    }

    /**
     * 获得魂骨星级
     * @param goodsInfo
     * @return
     */
    public static int getBoneStar(GoodsInfo goodsInfo) {
        if (goodsInfo == null) {
            return 0;
        }
        int star = goodsInfo.getParamList().get(1).intValue();
        return star;
    }

    /**
     * 获得魂骨共鸣点
     * @param goodsInfo
     * @return
     */
    public static int getBoneSuitStar(GoodsInfo goodsInfo) {
        if (goodsInfo == null) {
            return 0;
        }
        int suitStar = goodsInfo.getParamList().get(2).intValue();
        return suitStar;
    }

    /**
     * 获得魂骨孔位类型
     * @param goodsInfo
     * @return
     */
    public static int getBonePosType(GoodsInfo goodsInfo) {
        if (goodsInfo == null) {
            return 0;
        }
        int star = goodsInfo.getParamList().get(0).intValue();
        return star;
    }

}
